from operator import le
from genshin_elements import *
import pygame

ALL_CHEATERS=['paimon!', 'timegod!']


class CheatCode:
    name_list:list
    letter_stack:list
    time_lim: int
    time_remaining: list

    def __init__(self, name_list, time_lim) -> None:
        self.name_list = name_list
        self.letter_stack = [''] * len(name_list)
        self.time_lim = time_lim
        self.time_remaining = [0] * len(name_list)

    def time_pass(self):
        for j in range(len(self.time_remaining)):
            if(self.time_remaining[j]>0):
                self.time_remaining[j] -= 1
            else:
                self.letter_stack[j] = ''

    def add_letter(self, letter):
        for j in range(len(self.letter_stack)):
            if(self.letter_stack[j] == ''):
                if(self.name_list[j][0] == letter):
                    self.letter_stack[j] = letter
                    self.time_remaining[j] = self.time_lim

            else:
                if(letter == self.name_list[j][len(self.letter_stack[j])]):
                    self.letter_stack[j] += letter  
                else:
                    self.letter_stack[j] = ''
                    self.time_remaining[j] = 0

            if(len(self.letter_stack[j]) == len(self.name_list[j])):
                self.letter_stack = [''] * len(self.name_list)
                self.time_remaining = [0] * len(self.name_list)
                return j 

        return -1


class CheaterBase:
    name:str
    def __init__(self, name):
        self.name = name

    def __call__(self, app):
        pass

    def initialize(self, app):
        pass

    def event_call(self, event, app):
        return event

    def render(self, window: pygame.Surface):
        pass

class Paimon(CheaterBase):
    pos:tuple
    def __init__(self):
        super().__init__('paimon')
    
    def initialize(self, app):
        self.update_pos(app)
    
    def update_pos(self, app):
        if(app.pile.all_nodes):
            self.pos = app.pile.all_nodes[-1].pos
    
    def __call__(self, app):
        self.update_pos(app)

    def render(self, window: pygame.Surface):
        pygame.draw.rect(window, (255,0,0), (self.pos[0], self.pos[1], 10, 10))
        return super().render(window)

class TimeGod(Paimon):
    def __init__(self):
        super().__init__()
        self.name = 'time god'
    
    def initialize(self, app):
        app.capa_type = ELEMENT_TYPE.TIME
        return super().initialize(app)


    def __call__(self, app):
        if(app.game_phase != GAMEPHASE.WIN):
            app.capa.activate()
            app.capa.on_click()

        return super().__call__(app)

    def event_call(self, event, app):
        if(event.type == pygame.MOUSEBUTTONDOWN):
            event.pos = self.pos
        return super().event_call(event, app)

    
def set_cheater(name):
    if(name == 'paimon!'):
        return Paimon()
    elif(name == 'timegod!'):
        return TimeGod()
    else:
        return CheaterBase('none!')

if __name__ == '__main__':
    namelist = ['paimon!','none!','me!','lata!', 'lata movo dada!', 'avada kedavra!', 'movo!']

    cheatcode = CheatCode(namelist, 24)
    flag = True
    while (flag):
        c = input()
        id = cheatcode.add_letter(c)
        print('candidates:', cheatcode.letter_stack)
        print('time remaining', cheatcode.time_remaining)
        if(id >= 0): 
            print(namelist[id], 'Get!')

        time_pass = input('time pass:')
        for j in range(int(time_pass)):
            cheatcode.time_pass()

        if(c == '0'):
            flag = False